package com.vincent.controller.admin;

import com.vincent.common.enums.DefaultEnum;
import com.vincent.common.enums.ResultMapKey;
import com.vincent.common.enums.SessionEnum;
import com.vincent.common.utils.ResultMapUtil;
import com.vincent.model.AdminMenu;
import com.vincent.service.MenuService;
import com.vincent.vo.MenuSaveVO;
import com.vincent.vo.MenuUpdateVO;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Vincent on 2015/1/4.
 */
@Controller
@RequestMapping("/admin/menu")
public class MenuController {
    @Resource
    private MenuService menuService;
    @RequestMapping("/manage")
    public String manage(){
        return "/admin/menu/manage";
    }

    @RequestMapping(value = "/getMenuManageInfoAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody
    Object getMenuManageInfoAjax(){
        Map<String,Object> map=new HashMap<String, Object>();
        Map<String, Object> serviceMap = menuService.findMenus();
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            map.put(ResultMapKey.ERROR.getCode(),errorObj);
            return map;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        map.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return map;
    }
    @RequestMapping("/add")
    public String add(@RequestParam(required = false)Integer parentId,HttpServletRequest request){
        AdminMenu menu = new AdminMenu();
        menu.setParentid(parentId);
        menu.setIcon(DefaultEnum.MENUICON.getCode());
        request.setAttribute("menu",menu);
        return "/admin/menu/add";
    }

    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable Integer id,HttpSession session,HttpServletRequest request){
        Map<String, Object> serviceMap = menuService.getMenu(id);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/menu/manage.html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        request.setAttribute("menu",successObj);
        return "/admin/menu/edit";
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute MenuSaveVO vo,Errors errors,HttpSession session){
        Map<String, Object> serviceMap = menuService.saveMenu(vo);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/menu/add.html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        session.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
        return "redirect:/admin/menu/manage.html";
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute MenuUpdateVO vo,Errors errors,HttpSession session){
        Map<String, Object> serviceMap = menuService.updateMenu(vo);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            session.setAttribute(SessionEnum.ERRORMSG.getCode(),errorObj);
            return "redirect:/admin/menu/edit/"+vo.getMenuId()+".html";
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        session.setAttribute(SessionEnum.SUCCESSMSG.getCode(),successObj);
        return "redirect:/admin/menu/manage.html";
    }
    @RequestMapping(value = "/deleteAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody Object deleteAjax(@RequestParam("id[]") Integer []id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Map<String, Object> serviceMap = menuService.deleteMenus(id);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),errorObj);
            return resultMap;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return resultMap;
    }

}
